package com.yzd.xander.action;

import com.opensymphony.xwork2.ActionSupport;
import com.yzd.xander.entity.Group;
import com.yzd.xander.entity.History;
import com.yzd.xander.entity.QunInfo;
import com.yzd.xander.entity.Users;
import com.yzd.xander.service.GroupDataService;
import com.yzd.xander.service.HistoryService;
import com.yzd.xander.service.QunInfoService;
import org.apache.struts2.convention.annotation.*;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by YE on 2018-06-07.
 */
@Controller
@Scope("prototype")
@ParentPackage("struts-default")
@Namespace("/search")
@Results({@Result(name = "qq", location = "../qq.jsp"),
        @Result(name = "group", location = "../group.jsp"),
        @Result(name = "quninfo", location = "../quninfo.jsp")})
public class SearchAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {
    @Autowired
    private GroupDataService groupDataService;
    @Autowired
    private QunInfoService qunInfoService;
    @Autowired
    private HistoryService historyService;
    private HttpServletRequest request;
    private HttpServletResponse response;
    History history = null;
    Users user = null;
    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Action("qq")
    public String qq() {
        user = (Users) request.getSession().getAttribute("user");
        if (user != null) {
            try {
                int QQ = Integer.parseInt(request.getParameter("qq"));
                history = new History();
                history.setNum(QQ);
                history.setIsQq(1);
                history.setUid(user.getUid());
                history.setCreateDate(simpleDateFormat.format(new Date()));
                historyService.addHistory(history);
                long begintime = System.currentTimeMillis();
                List<Group> list = groupDataService.getByQQ(QQ);
                long endtime=System.currentTimeMillis();
                long costTime = (endtime - begintime)/1000;
                if (list != null) {
                    request.setAttribute("time",costTime);
                    request.setAttribute("list", list);
                } else {
                    request.setAttribute("message", "无此QQ信息");
                }
            } catch (Exception e) {
                e.printStackTrace();
                request.setAttribute("message", "请输入正确的QQ ps:QQ号<2147483647");
            }
        }
        return "qq";
    }

    @Action("group")
    public String group() {
        user = (Users) request.getSession().getAttribute("user");
        if (user != null) {
            try {
                int Qun = Integer.parseInt(request.getParameter("qun"));
                history = new History();
                history.setNum(Qun);
                history.setIsQq(0);
                history.setUid(user.getUid());
                history.setCreateDate(simpleDateFormat.format(new Date()));
                historyService.addHistory(history);
                long begintime = System.currentTimeMillis();
                List<Group> list = groupDataService.getByQun(Qun);
                long endtime=System.currentTimeMillis();
                long costTime = (endtime - begintime)/1000;
                if (list != null) {
                    request.setAttribute("time",costTime);
                    request.setAttribute("list", list);
                } else {
                    request.setAttribute("message", "无此群信息");
                }
            } catch (Exception e) {
                request.setAttribute("message", "请输入正确的群号");
            }
        }
        return "group";
    }

    @Action("quninfo")
    public String quninfo() {
        user = (Users) request.getSession().getAttribute("user");
        if (user != null) {
            try {
                int Qun = Integer.parseInt(request.getParameter("qun"));
                history = new History();
                history.setNum(Qun);
                history.setIsQq(0);
                history.setUid(user.getUid());
                historyService.addHistory(history);
                history.setCreateDate(simpleDateFormat.format(new Date()));
                long begintime = System.currentTimeMillis();
                QunInfo qunInfo = qunInfoService.getQunInfo(Qun);
                long endtime=System.currentTimeMillis();
                long costTime = (endtime - begintime)/1000;
                if (qunInfo != null) {
                    request.setAttribute("time",costTime);
                    request.setAttribute("qunInfo", qunInfo);
                } else {
                    request.setAttribute("message", "无此群信息");
                }
            } catch (Exception e) {
                request.setAttribute("message", "请输入正确的群号");
            }
        }
        return "quninfo";
    }

    @Override
    public void setServletRequest(HttpServletRequest httpServletRequest) {
        this.request = httpServletRequest;
    }

    @Override
    public void setServletResponse(HttpServletResponse httpServletResponse) {
        this.response = httpServletResponse;
    }
}
